<template>
    <rx-dialog @handOk="handleOk" @cancel="cancel">

        <div class="divBox">
            <a-alert :message="i18n('filter.warn','注意:过滤条件使用的是freemark语法')" type="info" />
            <div style="height: 100%;margin-top:10px">
                <rx-monaco-editor
                    ref="javaCode"
                    v-model="javaCode"
                    language="java"
                >
                    <template v-slot:monacoeditor>
                        <p>
                            <span>{{i18n('form.character','运算符')}}:</span>
                            <a-select style="width: 120px;" allowClear :options="operators"
                                      @change="addOperator"></a-select>

                            <span style="display: inline-block;margin-left: 10px;">{{ i18n('form.conditionField', '条件字段:') }}</span>
                            <a-select style="width: 120px;" allowClear :options="tableFields"
                                      v-model="tableField"></a-select>
                            <span style="display: inline-block;margin-left: 10px;">{{ i18n('list.value', '值') }}：</span>
                            <a-select style="width: 120px;" allowClear :options="valueFields"
                                      v-model="valueField"></a-select>
                            <a-button @click="onAdd" style="margin-left: 10px;">
                                {{ i18n("button.add", '添加') }}
                            </a-button>
                        </p>
                    </template>

                </rx-monaco-editor>
            </div>
        </div>

    </rx-dialog>
</template>

<script>
import {RxMonacoEditor, Util} from "@lowcode/jpaas-common-lib"
import FormBoEntityApi from "@/api/form/bo/formBoEntity"
import FormDbApi from "@/api/form/bo/formDb";

export default {
    name: "FormTableFormulaFilterSql",
    props: ["layerid", "destroy", "value", "boDefId", "entName", "tableName", "dsName"],
    components: {
        RxMonacoEditor
    },
    data() {
        return {
            valueData: "",
            tableFields: [],
            valueFields: [],
            tableField: "",
            valueField: "",
            javaCode: "",

            operators:[{label:"AND",value:"AND"},
                {label:"OR",value:"OR"},
                {label:"()",value:"()"},
                {label:this.i18n('filter.mainCur','源主表(当前)'),value:"mainCur"},
                {label:this.i18n('filter.mainOld',"源主表(原值)"),value:"mainOld"},
                {label:this.i18n('filter.cur',"源记录(当前)"),value:"cur"},
                {label:this.i18n('filter.old',"源记录(原值)"),value:"old"},
            ]
        }
    },
    created() {
        this.init();
    },
    methods: {
        i18n(name, text, key) {
            return this.$ti18(name, text, "formTableFormula", key);
        },
        init() {
            if (this.value) {
                this.javaCode = this.value;
            }
            this.loadTableFields("tableFields", this.tableName, this.dsName)
            this.loadBoAttrs("valueFields", this.boDefId, this.entName, true, true);
        },
        loadTableFields(comboxId, tableName, dsName) {
            if (!tableName){
                this.$message.warn(this.i18n('filter.noSelectTip','请先选择目标表'))
                return;
            }
            var self = this;
            FormDbApi.getByName("dsName=" + dsName + "&tbName=" + tableName).then(data => {
                self[comboxId] = data.columnList.map(item => {
                    return {label: item.comment, value: item.fieldName, columnType: item.columnType}
                });
            })
        },
        loadBoAttrs(comboxId, boDefId, entName, needCommonFields, showName) {
            var self = this;
            FormBoEntityApi.getFieldByBoDefId(boDefId, entName).then(data => {
                data = data.map(item => {
                    return {label: item.comment, value: item.name, isSingle: item.isSingle, control: item.control}
                })
                if (!showName) {
                    self[comboxId] = data;
                } else {
                    for (var i = 0; i < data.length; i++) {
                        var item = data[i];
                        if (item.isSingle == 0) {
                            var nameObj = {};
                            nameObj.value = item.value + "_name";
                            nameObj.label = item.label + "(" + this.i18n('form.name', '名称') + ")";
                            self[comboxId].push(nameObj);
                        }
                        self[comboxId].push(item);
                    }
                }
                if (needCommonFields) {
                    let arr = [
                        {"value": "ID_", "label": this.$ti18('rxForm.id', '主键')},
                        {"value": "REF_ID_", "label": this.i18n('isValueMap.associated', '关联字段')},
                        {"value": "INST_ID_", "label": this.i18n('isValueMap.instId', '流程实例ID')},
                        {"value": "PARENT_ID_", "label": this.i18n('isValueMap.parentField', '父ID')},
                        {"value": "INST_STATUS_", "label": this.i18n('isValueMap.instanceState', '流程实例状态')},
                        {"value": "CREATE_BY_", "label": this.$ti18('rxForm.createBy', '创建人')},
                        {"value": "CREATE_TIME_", "label": this.$ti18('rxForm.createTime', '创建时间')},
                        {"value": "UPDATE_BY_", "label": this.i18n('isValueMap.updateBy', '更新人')},
                        {"value": "UPDATE_TIME_", "label": this.$ti18('rxForm.updateTime', '更新时间')},
                        {"value": "TENANT_ID_", "label": this.i18n('isValueMap.tenantID', '租户ID')},
                        {"value": "CREATE_DEP_ID_", "label": this.i18n('isValueMap.departmentID', '部门ID')}
                    ]
                    for (let item of arr){
                        if(!self[comboxId].find(o=>o.value === item.value)){
                            self[comboxId].push(item)
                        }
                    }
                }
            })
        },
        getData() {
            return this.javaCode;
        },
        handleOk() {
            Util.closeWindow(this, "ok", this.javaCode);
        },
        cancel() {
            Util.closeWindow(this, "cancel");
        },
        onAdd() {
            var targetField = this.valueField;
            var srcField = this.tableField;
            var ary = this.tableFields;
            var content = srcField + "=";
            for (var i = 0; i < ary.length; i++) {
                var item = ary[i];
                if (item.value == srcField) continue;
                var cur = "$" + "{cur.";
                content += (item.columnType == "varchar") ? "'" + cur + targetField + "}'" : cur + targetField + "}";
                break;
            }
            this.insertScript(content);
        },
        insertScript(script){
            this.$refs.javaCode.insertContent(script);
        },
        addOperator(operator) {
            this.insertScript(operator);
        }

    }
}
</script>

<style scoped>
div.operatorContainer {
    padding: 5px;
    text-align: left;
}

div.operatorContainer > div {
    border-radius: 5px;
    border: 1px solid #dfdfdf;
    display: inline-block;
    background: #fff;
    padding: 0 10px;
    line-height: 31px;
    text-align: center;
    margin: 5px;
    min-width: 32px;
    font-size: 12px;
    color: #555;
    height: 32px;
    cursor: pointer;
}

div.operatorContainer > div:hover {
    background: #f4f4f4;
}

.conditionBox {
    padding: 6px;
    box-sizing: border-box;
}

.divBox {
    height: 300px;
}

header {
    font-size: 14px;
    font-weight: bold;
    position: relative;
    padding-left: 10px;
}

header:before {
    content: " ";
    width: 3px;
    height: 14px;
    background-color: #1890ff;
    display: inline-block;
    position: absolute;
    top: 4px;
    left: 0px;
    border-radius: 2px 0 0 2px;
}
</style>